package com.welsy.user.util
{
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.welsy.authoring.event.GetTestEvent;
	import com.welsy.authoring.vo.course.ElementVO;
	import com.welsy.authoring.vo.course.Question;
	import com.welsy.iter.vo.iter.IterElementVO;
	import com.welsy.user.event.GetTestTrackEvent;
	import com.welsy.user.event.SaveElementTrackEvent;
	import com.welsy.user.event.SaveIterElementTrackEvent;
	import com.welsy.user.event.SaveQuestionTrackEvent;
	import com.welsy.user.model.UserModelLocator;
	
	import mx.core.Application;
	
	public class TrackService
	{
		private var trackUser:Boolean
		
		public function TrackService()
		{		
			//lato amministrativo non ha il track
			if (mx.core.Application.application.applicationType == mx.core.Application.application.ADMIN_APPLICATION) 
				trackUser = false
			else
				trackUser = true
		}
		
		public function TrackIterElement(iterElement:IterElementVO,status:int,isLast:Boolean):void
		{
			if (trackUser) {
				var trackEvent : SaveIterElementTrackEvent = new SaveIterElementTrackEvent()
				trackEvent.iterElementId = Number(iterElement.iterElementId)
				trackEvent.status = status.toString()
				trackEvent.isLast = isLast
				CairngormEventDispatcher.getInstance().dispatchEvent(trackEvent);
			}
		}
		
		public function TrackElement(element:ElementVO,isLast:Boolean):void
		{
			if (trackUser) {
				var trackEvent : SaveElementTrackEvent = new SaveElementTrackEvent()
				trackEvent.element = element
				trackEvent.isLast = isLast
				CairngormEventDispatcher.getInstance().dispatchEvent(trackEvent);
			}
		}
		
		public function TrackQuestion(question:Question,isLast:Boolean):void
		{
			if (trackUser) {
				var saveTrackEvent:SaveQuestionTrackEvent = new SaveQuestionTrackEvent()
				saveTrackEvent.question = question
				saveTrackEvent.isLast = isLast
				CairngormEventDispatcher.getInstance().dispatchEvent( saveTrackEvent );	
			}
		}
		
		public function TrackGetTestEvent(testId:String):void
		{
			if (trackUser) {
				var customTestTrackEvent:GetTestTrackEvent = new GetTestTrackEvent(GetTestTrackEvent.EVENT_GET_TEST_TRACK);		  
				customTestTrackEvent.testID = int(testId);
				customTestTrackEvent.userId = int(UserModelLocator.getInstance().user.UserId);
				
				CairngormEventDispatcher.getInstance().dispatchEvent(customTestTrackEvent)
			}
			else {
				var customTestEvent:GetTestEvent = new GetTestEvent(GetTestEvent.EVENT_GET_TEST);		  
				customTestEvent.testID = int(testId);
				
				CairngormEventDispatcher.getInstance().dispatchEvent(customTestEvent)
			}
		}
	}
}